<?php
/**
 * @name 会员列表调用接口
 * @Author LiuYi
 * @Created 2016-11-09
 * @Copyright MEP Fiidee Monda CN
 */
namespace api\service;

use api\service\interfaces\IMemberApiService;
use herosphp\bean\Beans;
use herosphp\core\Loader;
use herosphp\core\Log;
use herosphp\utils\AjaxResult;
use utils\session\MySessionHandler;

Loader::import('api.service.interfaces.IMemberApiService');
Loader::import('utils.session.MySessionHandler');
class MemberApiService implements IMemberApiService {

    private $__service;
    public function __construct()
    {
        $this -> __service = Beans::get("member.member.service");

    }

    /**
     * @see \api\service\interfaces\IMemberApiService::getMembers()
     */
    public function getMembers($conditions, $order=null, $page=1, $pagesize=10)
    {
        if( $conditions ) {
            if ( !$this -> checkParams($conditions,"123")) {
                return new AjaxResult(AjaxResult::OP_FAILURE, "error params, conditions must be array", null);
            }
        }
        $items = $this -> __service -> getMembers( $conditions, "*", $order, array($page,$pagesize));
        $count = $this -> __service -> getTotalWithOutAppId($conditions);
        $result =  new AjaxResult(AjaxResult::OP_SUCCESS,"success",null);
        $result -> putItems($items);
        $result -> putCount($count);
        return $result;
    }

    /**
     * 回写
     * @param $data
     * @param $userid
     * @return AjaxResult
     */
    public function updateMember($data, $userid){
        if( !$this -> checkParams($data,$userid) ){
            return new AjaxResult(AjaxResult::OP_FAILURE, "error params", null);
        }
        $conditions['dasu_pay_id'] = $userid;
        $bool = $service = $this -> __service -> updatesWithOutAppId($data, $conditions);
        if( $bool ){
            if( $data['ischecked'] ) {
                $this->updateMemberSession($userid, 'dasuinfo');
            }
            return new AjaxResult(AjaxResult::OP_SUCCESS,"success",null);
        }else{
            return new AjaxResult(AjaxResult::OP_FAILURE,"fail",null);
        }
    }

    /**
     * 外部更新session方法
     */
    public function updateSessionApi($userid, $key){
        if( !$userid || !$key ){
            return new AjaxResult(AjaxResult::OP_FAILURE,"error params",null);
        }
        if( !in_array( $key, array('dasuinfo','company') ) ){
            return new AjaxResult(AjaxResult::OP_FAILURE,"error key",null);
        }
        $this -> updateMemberSession($userid,$key);
        return new AjaxResult(AjaxResult::OP_SUCCESS,"success",null);
    }

    /**
     * 检测参数是否合法
     * @param array $array
     * @param $empty
     * @return bool
     */
    protected function checkParams(array $array,$empty){
        if( !is_array($array) ){
            return false;
        }
        if( $empty == "" ){
            return false;
        }
        return true;
    }

    /**
     * 更新当前用户的session,
     * @param string $userid 当前会员登陆的session对应的大塑支付id
     * @param string $key 需要清除的key，目前支持dasuinfo，company
     * @return bool
     */
    protected function updateMemberSession($userid, $key){

        if ( $userid == "" ){
            return true;
        }
        $sessions = $this -> __service -> getSessionWithOutAppId(array('dasu_pay_id'=>$userid));
        if ( !$sessions ){
            return true;
        }
        if( !in_array( $key, array('dasuinfo','company') ) ){
            return true;
        }
        $save_path = APP_RUNTIME_PATH."session/".APP_NAME;
        $prefix = "heros_session_";
        $handler = new MySessionHandler();
        session_set_save_handler($handler, true);
        session_start();
        foreach ($sessions as $value){
            $handler -> open($save_path,$prefix.$value['session_id']);
            $res = $handler ->read($prefix.$value['session_id']);
            session_decode($res);
            unset($_SESSION[$key]);
            $out = session_encode();
            $handler -> write($prefix.$value['session_id'],$out);
        }
        return true;
    }

}
